import {
	observable,
	action
} from 'mobx';
import * as Api from '../apis/hrmgroup';
import {
	WeaForm
} from 'comsMobx';
import {
	WeaTableNew
} from 'comsMobx'
import {
	message,
	Modal
} from 'antd'
import * as mobx from 'mobx';
import {i18n} from '../public/i18n';
import {WeaLocaleProvider} from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;

const toJS = mobx.toJS;
const {
	TableStore
} = WeaTableNew;
const confirm = Modal.confirm;

export class HrmGroup {
	@observable editTabKey = '0';
	@observable suggestTabKey = '3';
	@observable condition1 = [];
	@observable condition2 = [];
	@observable condition3 = [];
	@observable condition4 = [];
	@observable condition5 = [];
	@observable condition6 = [];
	@observable condition7 = [];
	@observable isSuggestDialogShow = false;
	@observable isPanelOneShow = false;
	@observable isPanelTwoShow = false;
	@observable isPanelThreeShow = false;
	@observable isNewDialogShow = false;
	@observable isEditDialogShow = false;
	@observable isMadalShow = false;
	@observable isImportBaseDialogShow = false;
	@observable isImportMemberDialogShow = false;
	@observable isShareDialogShow = false;
	@observable isImportInfoDialogShow = false;
	@observable isImportHistoryDialogShow = false;
	@observable isImportMemInfoDialogShow = false;
	@observable form1 = new WeaForm();
	@observable form2 = new WeaForm();
	@observable form3 = new WeaForm();
	@observable form4 = new WeaForm();
	@observable form5 = new WeaForm();
	@observable form6 = new WeaForm();
	@observable form7 = new WeaForm();
	@observable tableStore1 = new TableStore();
	@observable tableStore2 = new TableStore();
	@observable tableStore3 = new TableStore();
	@observable tableStore4 = new TableStore();
	@observable groupid = '';
	@observable memberid = '';
	@observable typeid = '';
	@observable excelfile = '';
	@observable list = [];
	@observable upStatus = '';
	@observable importStatus = '';
	@observable currentIndex = '0';
	@observable logFilePath = '';
	@observable loading = true;
	@observable columns = [];
	@observable dataSource = [];
	@observable action = {};
	@observable delFileNum = '';
	@observable suggestid = '';
	@observable hasTrumpet = false;
	@observable hasHint = false;
	@observable height = 510;
	@observable orderArr = [];
	@observable rowIdArr = [];
	@observable valArr = [];
	@observable checkBoxVal = 0;
	@observable hasWeaTop = false;
	@observable selectvalue = '1';
	@observable scopeValue = [10, 100];
	@observable isImportStop = false;
	@observable hasAuthority = false;
	@observable showCanceled = false;

	@action
	updateFormFields = (arg, val) => {
		if (arg == '1') this.form1.updateFields({
			name: {
				value: val
			}
		});
		if (arg == '2') this.form2.updateFields({
			resourcename: {
				value: val
			}
		});
		if (arg == '3') this.form4.updateFields({
			excelfile: {
				value: val
			}
		});
		if (arg == '4') this.form5.updateFields({
			excelfile: {
				value: val
			}
		});
		if (arg == '5') this.form7.updateFields({
			suggesttitle: {
				value: val
			}
		});
		if (arg == '6') this.form6.updateFields({
			seclevel__seclevelTo: {
				value: val
			}
		});
	}

	tabClick = (key) => {
		this.editTabKey = key;
		if (key == '0') {
			this.getPanelForm('1');
		}
		if (key == '1') {
			this.getPanelForm('2');
			this.getTableInfo('2');
		}
		if (key == '2') {
			this.getTableInfo('3');
		}
	}

	suggestTabClick = (key) => {
		this.suggestTabKey = key;
		this.getTableInfo('7');
	}

	tabReset = () => {
		this.editTabKey = '0';
	}

	getPanelForm = (arg) => {
		let params;
		if (arg == '1') params = {
			cmd: 'base'
		};
		if (arg == '2') params = {
			cmd: 'member'
		};
		if (arg == '3') params = {
			cmd: 'suggest'
		};

		Api.getSearchCondition(params).then((data) => {
			if (arg == '1') {
				this.condition1 = data.condition;
				this.form1.initFormFields(data.condition);
			}
			if (arg == '2') {
				this.condition2 = data.condition;
				this.form2.initFormFields(data.condition);
			}
			if (arg == '3') {
				this.condition7 = data.condition;
				this.form7.initFormFields(data.condition);
			}
		});
	}

	getForm = (arg) => {
		let params;
		if (arg == '1' || arg == '2') {
			params = {
				cmd: 'base'
			};
		}
		if (arg == '2') {
			params.groupid = this.groupid;
		}
		if (arg == '3') {
			params = {
				cmd: 'importbase'
			};
		}
		if (arg == '4') {
			params = {
				cmd: 'importmember'
			};
		}
		if (arg == '5') {
			params = {
				cmd: 'share'
			};
		}

		Api.getFormCondition(params).then((data) => {
			if (arg == '1' || arg == '2') {
				this.form3 = new WeaForm();
				this.condition3 = data.condition;
				this.form3.initFormFields(data.condition);
			}
			if (arg == '1') {
				this.setNewDialogStatus(true);
			}
			if (arg == '2') {
				this.setNewDialogStatus(false);
				this.setEditDialogStatus(true);
			}
			if (arg == '3') {
				this.condition4 = data.condition;
				this.form4.initFormFields([data.condition[0]]);
				this.setImportBaseDialogStatus(true);
			}
			if (arg == '4') {
				this.condition5 = data.condition;
				this.form5.initFormFields([data.condition[0]]);
				this.setImportMemberDialogStatus(true);
			}
			if (arg == '5') {
				data.condition[0].items = this.putElementToLast('rolelevel', data.condition[0].items);
				this.condition6 = data.condition;
				this.form6.initFormFields(data.condition);
				this.setShareDialogStatus(true);
			}
		})
	}

	changeShowAll = () => {
		this.showCanceled = !this.showCanceled;
		this.getTableInfo("1");
	}

	doCancel = (params = {}) => {
		let _this = this;
	    confirm({
	      title: i18n.confirm.defaultTitle(),
	      content: i18n.label.sealConfirm(),
	      okText: i18n.button.ok(),
	      cancelText: i18n.button.cancel(),
	      onOk() {
	        Api.doCancel(params).then(data => {
				  if (data.status == '1') {
					_this.getTableInfo("1");
					// message.success(i18n.message.cancelSuccess());
				  } else {
					message.warning(data.message);
				  }
				}, error => {
				  message.warning(error.message);
				})
	      },
	      onCancel() {
	        return false;
	      },
	    });
		
	  }
	
	doISCanceled = (params = {}) => {
		let _this = this;
	    confirm({
	      title: i18n.confirm.defaultTitle(),
	      content: i18n.label.unsealConfirm(),
	      okText: i18n.button.ok(),
	      cancelText: i18n.button.cancel(),
	      onOk() {
	      	Api.doISCanceled(params).then(data => {
				if (data.status == '1') {
					_this.getTableInfo("1");
					// message.success(i18n.message.doISCancelSuccess());
				} else {
					message.warning(data.message);
				}
			}, error => {
				message.warning(error.message);
			})
	      },
	      onCancel() {
	        return false;
	      },
	    });
	}

	getTableInfo = (arg) => {
		let params;
		if (arg == '1') {
			params = {
				cmd: 'base',
				showCanceled : this.showCanceled ? '1' : '0',
				...this.form1.getFormParams()
			};
		}
		if (arg == '2') {
			params = {
				cmd: 'member',
				groupid: this.groupid,
				...this.form2.getFormParams()
			};
		}
		if (arg == '3') {
			params = {
				cmd: 'share',
				groupid: this.groupid
			};
		}
		if (arg == '4') {
			params = {
				cmd: 'importbaseresult',
				currentIndex: this.currentIndex
			};
		}
		if (arg == '5') {
			params = {
				cmd: 'importhistory',
				importtype: 'group'
			};
		}
		if (arg == '6') {
			params = {
				cmd: 'importmemberresult',
				currentIndex: this.currentIndex
			};
		}
		if (arg == '7') {
			params = {
				cmd: 'suggest',
				status: (parseInt(this.suggestTabKey) - 3).toString(),
				...this.form7.getFormParams()
			};
		}

		Api.getSearchResult(params).then((data) => {
			if (data.status == '-1' && arg !== '5') {
				if (data.message) message.warning(data.message);
				this.loading = false;
				return false;
			}
			if (data.status == '-1' && arg == '5') {
				this.setAuthorityStatus(false);
				this.loading = false;
				return false;
			}
			this.setAuthorityStatus(true);
			if (arg == '1') {
				this.tableStore1.getDatas(data.sessionkey, 1);
			}
			if (arg == '2') {
				this.tableStore2.getDatas(data.sessionkey, 1);
			}
			if (arg == '3') {
				this.tableStore3.getDatas(data.sessionkey, 1);
			}
			if (arg == '4' || arg == '6') {
				let is = data.importStatus;
				this.importStatus = is;
				if (is == 'over' && data.logFilePath) {
					this.logFilePath = data.logFilePath;
				}
				if (data.items) {
					this.dataSource = this.dataSource.concat(data.items);
					this.currentIndex = this.dataSource.length;
				}
				this.columns = data.columns;
			}
			if (arg == '5') {
				data.columns[0].width = '40%';
				data.columns[1].width = '40%';
				data.columns[2].width = '20%';
				if (data.items.length == 0) this.loading = false;
				this.columns = data.columns;
				this.dataSource = data.items;
				this.action = data.action;
			}
			if ((arg == '4' || arg == '5' || arg == '6') && data.items && data.items.length > 0) {
				this.loading = false;
			}
			if (arg == '7') {
				this.tableStore4.getDatas(data.sessionkey, 1);
			}
		})
	}

	save = (arg) => {
		let params;
		if (arg == '1' || arg == '2' || arg == '9') {
			params = this.form3.getFormParams();
			if (!params.name) {
				message.warning(getLabel('386497',"必填信息不完整！"));
				return false;
			}
			params = {
				cmd: 'base',
				...params
			};
		}
		if (arg == '9') {
			params = {
				groupid: this.groupid,
				...params
			};
		}

		if (arg == '3') {
			params = {
				cmd: 'member',
				groupid: this.groupid,
				members: this.memberid
			};
		}

		if (arg == '4') {
			params = this.form6.getFormParams();
			let sharetype = params.sharetype;
			let jobtitlelevel = params.jobtitlelevel;

			if ((sharetype == '2' || sharetype == '3' || sharetype == '4' || sharetype == '8' || sharetype == '5') && (!('seclevel' in params) || !('seclevelTo' in params))) {
				params.seclevel = this.scopeValue[0];
				params.seclevelTo = this.scopeValue[1];
			}

			if ((sharetype == '1' && !params.rsid) || (sharetype == '2' && (!params.sbid || params.seclevel == undefined || params.seclevelTo == undefined)) || (sharetype == '3' && (!params.did || params.seclevel == undefined || params.seclevelTo == undefined)) || (sharetype == '4' && !params.rid) || (sharetype == '7' && !params.jobtitle) || (sharetype == '7' && jobtitlelevel == '1' && !params.jobtitledepartment) || (sharetype == '7' && jobtitlelevel == '2' && !params.jobtitlesubcompany) || (sharetype == '8' && !params.customid)) {
				message.warning(getLabel('386497',"必填信息不完整！"));
				return false;
			}

			params = {
				cmd: 'share',
				groupid: this.groupid,
				...params,
				'alllevel': this.checkBoxVal,
				'rolelevel': (parseInt(this.selectvalue) - 1).toString()
			};
		}
		if (arg == '5') {
			params = {
				cmd: 'importbase',
				importtype: 'group',
				creater: '',
				...this.form4.getFormParams()
			};
		}

		if (arg == '6') {
			params = {
				cmd: 'importmember',
				groupid: this.groupid,
				...this.form5.getFormParams()
			};
		}

		if (arg == '5' || arg == '6') {
			if (this.importStatus == 'over') this.importStatusReset();
			if (this.isImportStop) this.importStopReset();
			setTimeout(() => this.timer(arg), 1000);
		}

		if (arg == '7') {
			params = {
				cmd: 'doAdd',
				id: this.suggestid
			};
		}

		if (arg == '8') {
			params = {
				cmd: 'doChange',
				id: this.suggestid
			};
		}

		if (arg == '10') {
			let arr = this.dataFilter(toJS(this.rowIdArr), toJS(this.valArr));
			for (let i = 0; i < arr.length; i++) {
				if (parseInt(arr[i]) < 0) {
					message.warning(getLabel('386498',"排序值不能小于0！"));
					return false;
				}
			}
			let result = this.dataFilter(toJS(this.rowIdArr), toJS(this.orderArr));
			params = {
				cmd: 'saveDspOrder',
				dspOrder: result
			};
		}

		Api.saveForm(params).then((data) => {
			if (data.status == '1') {
				if (data.message) message.success(data.message);
				if (data.groupid) {
					this.groupid = data.groupid;
				}
				if (arg == '1' || arg == '2') {
					this.setNewDialogStatus(false);
					this.getTableInfo('1');
				}
				if (arg == '2') {
					this.setEditDialogStatus(true);
					this.getForm('2');
				}
				if (arg == '3') {
					this.getTableInfo('1');
					this.getTableInfo('2');
				}
				if (arg == '4') {
					this.setShareDialogStatus(false);
					this.getTableInfo('3');
					this.shareFormReset();
				}

				if (arg == '7') {
					this.getTableInfo('7');
					this.getTableInfo('1');
					this.getTableInfo('2');
				}
				if (arg == '8') {
					this.getTableInfo('7');
				}
				if (arg == '9') {
					this.setEditDialogStatus(false);
					this.getTableInfo('1');
				}
				if (arg == '10') {
					this.setEditDialogStatus(false);
					this.getTableInfo('1');
				}
			} else {
				if (data.message) message.warning(data.message);
				return false;
			}
		})
	}


	putElementToLast = (domkey, arr) => {
		let index = this.getIndex(domkey, arr);
		if (index) {
			let ele = arr.splice(index, 1);
			arr.push(ele[0]);
		} else {
			return false;
		}
		return arr;
	}

	getIndex = (domkey, arr) => {
		let index;
		for (let i = 0; i < arr.length; i++) {
			if (domkey == arr[i].domkey[0]) {
				index = i;
			}
		}

		return index;
	}



	delete = (arg, tag1, tag2) => {
		let params;
		if (arg == '1') {
			params = {
				cmd: 'base',
				groupid: this.groupid
			};
		}
		if (arg == '2') {
			params = {
				cmd: 'member',
				groupid: this.groupid,
				id: this.memberid
			};
		}
		if (arg == '3') {
			params = {
				cmd: 'share',
				groupid: this.groupid,
				id: this.typeid
			};
		}
		if (arg == '4') {
			params = {
				cmd: 'importhistory',
				filename: this.delFileNum
			}
		}
		if (arg == '5') {
			params = {
				cmd: 'doDel',
				id: this.suggestid
			}
		}

		Api.deleteTableRow(params).then((data) => {
			if (tag1 == 'batch' && tag2 == 'last') {
				if (data.status == '1') {
					if (data.message) message.success(data.message);
					this.getTableInfo('1');
					if (arg == '2') {
						this.getTableInfo('2');
					}
					if (arg == '3') {
						this.getTableInfo('3');
					}

				} else {
					if (data.message) message.warning(data.message);
					return false;
				}
			}
			if (tag1 !== 'batch' && data.status == '1') {
				if (data.message) message.success(data.message);
				this.getTableInfo('1');
				if (arg == '2') {
					this.getTableInfo('2');
				}
				if (arg == '3') {
					this.getTableInfo('3');
				}
				if (arg == '4') {
					this.getTableInfo('5');
				}
				if (arg == '5') {
					this.getTableInfo('7');
					this.getTableInfo('2');
				}
			} else {
				if (data.message) message.warning(data.message);
				return false;
			}
		})
	}

	hasSuggestHint = () => {
		let params = {
			cmd: 'isHasSuggest'
		};
		Api.getSearchResult(params).then((data) => {
			if (data.isHas == 1) {
				this.setTrumpet(true);
				this.getPanelForm('3');
				this.getTableInfo('7');
			} else {
				this.setTrumpet(false);
			}

			if (data.isHasNew == 1) {
				this.setHint(true);
			} else {
				this.setHint(false);
			}
		})
	}

	timer = (arg) => {
		let t = setInterval(() => {
			if (this.importStatus == 'over') {
				clearInterval(t);
				this.importStatusReset();
				this.currentIndexReset();
				if (!this.isImportStop) {
					message.success(getLabel('386988',"导入完成！"));
				}
				this.getTableInfo('2');
				this.getTableInfo('1');
				return false;
			}
			arg == '5' ? this.getTableInfo('4') : this.getTableInfo('6');
		}, 1000);
	}

	dataFilter = (arr, _arr) => {
		let _arr_ = [];
		for (let i = 0; i < arr.length; i++) {
			if (arr.indexOf(arr[i]) == arr.lastIndexOf(arr[i])) {
				if (_arr_.indexOf(_arr[i]) == -1) {
					_arr_.push(_arr[i]);
				}

			} else {
				let index = arr.lastIndexOf(arr[i]);
				if (_arr_.indexOf(_arr[index]) == -1) {
					_arr_.push(_arr[index]);
				}
			}
		}

		return _arr_;
	}

	showConfirm = (arg, bool) => {
		let _this = this;
		confirm({
			title: i18n.confirm.defaultTitle(),
			content: getLabel('386499',"文件导入未完成，您确定要关闭当前弹出框吗?"),
			okText: i18n.button.ok(),
			cancelText: i18n.button.cancel(),
			onOk() {
				if (arg == '1') _this.isImportInfoDialogShow = false;
				if (arg == '2') _this.isImportMemInfoDialogShow = false;
				_this.loadingReset();
				_this.colAndDsReset();
				_this.logFilePathReset();
				if (_this.importStatus == 'importing') {
					_this.importStatus = 'over';
					_this.setImportStopStatus(true);
				}

			},
			onCancel() {
				return false;
			},
		});
	}

	setSuggestDialogStatus = (bool) => {
		this.isSuggestDialogShow = bool;
		this.hasSuggestHint();
		this.form7.reset();
	}

	setPanelOneStatus = (bool) => {
		this.isPanelOneShow = bool;
	}

	setPanelTwoStatus = (bool) => {
		this.isPanelTwoShow = bool;
	}

	setPanelThreeStatus = (bool) => {
		this.isPanelThreeShow = bool;
	}

	setNewDialogStatus = (bool) => {
		this.isNewDialogShow = bool;
	}

	setEditDialogStatus = (bool) => {
		this.isEditDialogShow = bool;
		this.form2.reset();
	}

	setGroupId = (groupid) => {
		this.groupid = groupid;
	}

	setMemberId = (memberid) => {
		this.memberid = memberid;
	}

	setTypeId = (typeid) => {
		this.typeid = typeid;
	}

	setMadalStatus = (bool) => {
		this.isMadalShow = bool;
	}

	setWeaTopStatus = (bool) => {
		this.hasWeaTop = bool;
	}

	setImportBaseDialogStatus = (bool) => {
		this.isImportBaseDialogShow = bool;
		if (!bool) {
			this.upStatusReset();
			this.excelfileReset();
			this.listReset();
		}
	}

	setImportMemberDialogStatus = (bool) => {
		this.isImportMemberDialogShow = bool;
		if (!bool) {
			this.upStatusReset();
			this.excelfileReset();
			this.listReset();
		}
	}

	setShareDialogStatus = (bool) => {
		this.isShareDialogShow = bool;
		this.shareFormReset();
	}

	setImportInfoDialogStatus = (bool) => {
		if (bool) {
			this.isImportInfoDialogShow = true;
		} else {

			if (this.importStatus == 'importing') {
				this.showConfirm('1', false);
			} else {
				this.isImportInfoDialogShow = false;
				this.importStatus = 'over';
				this.setImportStopStatus(false);
				this.loadingReset();
				this.colAndDsReset();
				this.logFilePathReset();
			}

		}
	}

	setImportHistoryDialogStatus = (bool) => {
		this.isImportHistoryDialogShow = bool;
		if (!bool) {
			this.loadingReset();
			this.colAndDsReset();
			this.actionReset();
		}
	}

	setImportMemInfoDialogStatus = (bool) => {
		if (bool) {
			this.isImportMemInfoDialogShow = true;
		} else {
			if (this.importStatus == 'importing') {
				this.showConfirm('2', false);
			} else {
				this.isImportMemInfoDialogShow = false;
				this.importStatus = 'over';
				this.setImportStopStatus(false);
				this.loadingReset();
				this.colAndDsReset();
				this.logFilePathReset();
			}
		}
	}

	setImportStopStatus = (bool) => {
		this.isImportStop = bool;
	}

	setExcelfile = (excelfile) => {
		this.excelfile = excelfile;
	}

	setList = (list) => {
		this.list = list;
	}

	setUpStatus = (upStatus) => {
		this.upStatus = upStatus;
	}

	setDelFileNum = (delFileNum) => {
		this.delFileNum = delFileNum;
	}

	setSuggestId = (suggestid) => {
		this.suggestid = suggestid;
	}

	setTrumpet = (bool) => {
		this.hasTrumpet = bool;
	}

	setHint = (bool) => {
		this.hasHint = bool;
	}

	setAuthorityStatus = (bool) => {
		this.hasAuthority = bool;
	}

	setDsporderArr = (val, rowData) => {
		if (val < 0) {
			message.warning(getLabel('386498',"排序值不能小于0！"));
		}
		this.rowIdArr.push(rowData.id);
		this.orderArr.push(rowData.id + '-' + val);
		this.valArr.push(val);
	}

	setImportStatus = (status) => {
		this.importStatus = status;
	}

	setCheckBoxVal = (val) => {
		this.checkBoxVal = val;
	}

	setSelectValue = (val) => {
		this.selectvalue = val;
	}

	setScopeValue = (scopeValue) => {
		this.scopeValue = scopeValue;
		this.updateFormFields('6', scopeValue);
	}

	loadingReset = () => {
		this.loading = true;
	}

	importStatusReset = () => {
		this.importStatus = '';
	}

	colAndDsReset = () => {
		this.columns = [];
		this.dataSource = [];
	}

	actionReset = () => {
		this.action = {};
	}

	upStatusReset = () => {
		this.upStatus = '';
	}

	logFilePathReset = () => {
		this.logFilePath = '';
	}

	currentIndexReset = () => {
		this.currentIndex = '0';
	}

	excelfileReset = () => {
		this.excelfile = '';
	}

	listReset = () => {
		this.list = [];
	}

	selectValueReset = () => {
		this.selectvalue = '1';
	}

	scopeValueReset = () => {
		this.scopeValue = [10, 100];
	}

	checkBoxValReset = () => {
		this.checkBoxVal = 0;
	}

	shareFormReset = () => {
		this.form6.reset();
		this.form6.updateFields({
			sharetype: {
				value: '1'
			}
		});
		this.checkBoxValReset();
		this.selectValueReset();
		this.scopeValueReset();
	}

	importStopReset = () => {
		this.isImportStop = false;
	}

	changeHeight = (height) => {
		this.height = height;
	}

}